package irc.view;

import irc.controller.FontController;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerListModel;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;



/** This class shows the font option panel
 * @author Dybdal
 * @version 1.1
 */
public class FontView {
	private JFrame frame;
	private JButton ok;
	private JButton cancel;
	private JButton defaults;
	private JButton color;
	private JRadioButton message;
	private JRadioButton event;
	private JRadioButton highlight;
	private JComboBox<String> font;
	private ButtonGroup group;
	private JCheckBox bold;
	private JCheckBox italic;
	private JSpinner fontSize;
	private FontController fc;

	/** constructor that gets the Font Controller
	 * @author Dybdal
	 * @since 1.1
	 */
	public FontView(FontController fc) {
		this.fc = fc;
	}
	
	/** creates the frame and panels
	 * @author Dybdal
	 * @since 1.1
	 */
	public void openFontOptions(){
		fc.fontDefault(); 	// sets all vaules to default
		frame = new JFrame("Font options"); // creates the font frame
		frame.setBounds(400, 200, 400, 400); // sets the frames size
		frame.setVisible(true);

		JPanel fontPanel = new JPanel(new BorderLayout());	//create the mainpanel
		GridBagConstraints gbc = new GridBagConstraints();  // sets layout and constraints
		GridBagLayout layout = new GridBagLayout();
		fontPanel.setLayout(layout);
		
		gbc.insets = new Insets (8, 8, 8, 8); // sets the spaceing
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.fill = GridBagConstraints.NONE;
		JPanel topMenu = createTopPanel(); // creates the top panel ( Jradiobuttons)
		layout.setConstraints (topMenu, gbc);
		fontPanel.add(topMenu, gbc); // adds top panel to main panel
		gbc.insets = new Insets (2, 2, 2, 2);
		gbc.gridy = 1;
		JButton color = createColorPanel(); // creates color panel
		layout.setConstraints (color, gbc);
		fontPanel.add(color, gbc); // adds color panel to main panel
		gbc.gridy = 2;
		JComboBox<String> font = createFontSelector(); // creates combobox for fonts
		layout.setConstraints (font, gbc);
		fontPanel.add(font, gbc); // adds combobox to mainpanel
		gbc.gridy = 3;
		JPanel fontInfo = createFontInfo(); // creates size bold and italic panel
		layout.setConstraints (fontInfo, gbc);
		fontPanel.add(fontInfo, gbc); // adds the panel to mainpanel
		gbc.gridy = 4;
		gbc.insets = new Insets (25, 25, 25, 25); // creates a preview label
		JLabel preview = new JLabel("This is how it looks like", JLabel.CENTER);
		preview.setOpaque(true);
		preview.setBackground(Color.WHITE);
		preview.setPreferredSize(new Dimension(200,50));
		layout.setConstraints (preview, gbc);
		fontPanel.add(preview, gbc); // adds the preview to mainpanel
		gbc.gridy = 5;
		JPanel bottomMenu = createBottomPanel(); // creates bottom panel, ok, cancel
		layout.setConstraints (bottomMenu, gbc);
		fontPanel.add(bottomMenu, gbc); // adds it to the main panel
		frame.getContentPane().add(fontPanel); // add main panel to frame
		
		// sets actionlisteneres to the components
		fc.fontActionlisteners(ok, cancel, defaults, color, font, message, event, highlight, bold, italic, preview, fontSize);
	}

	/** creates the the top panel
	 * @author Dybdal
	 * @since 1.1
	 * @return Jpanel
	 */
	public JPanel createTopPanel() {
		message = new JRadioButton("Message text");
		event = new JRadioButton("Event text");
		highlight = new JRadioButton("Highlight text");
		ButtonGroup group = new ButtonGroup(); // creates a buttongroup for the radiobuttons
		group.add(message);
		group.add(event);
		group.add(highlight);
		JPanel topMenu = new JPanel();
		topMenu.add(message, BorderLayout.EAST);
		topMenu.add(event, BorderLayout.CENTER);
		topMenu.add(highlight, BorderLayout.WEST);
		return topMenu;
	}

	/** creates the color button
	 * @author Dybdal
	 * @since 1.1
	 * @return Jbutton
	 */
	public JButton createColorPanel() {
		color = new JButton("Font Color");
		return color;
	}
	
	/** creates the font selector
	 * @author Dybdal
	 * @since 1.1
	 * @return Jcombobox
	 */
	public JComboBox<String> createFontSelector()	{
		font = new JComboBox<String>(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames());
		return font;
	}
	
	/** creates the font size, bold and italic selector
	 * @author Dybdal
	 * @since 1.1
	 * @return Jpanel
	 */
	public JPanel createFontInfo()	{
		JPanel fontInfo = new JPanel();
		Integer fontSizes[] = {8, 10, 12, 14, 16, 18, 24, 32, 64};
		SpinnerListModel fontSizeModel = new SpinnerListModel(fontSizes);
		fontSize = new JSpinner (fontSizeModel);
		fontSize.setValue(12);
		
		bold = new JCheckBox();
		italic = new JCheckBox();
		
		JLabel size = new JLabel("Font size");
		JLabel bolded = new JLabel("Bolded text");
		JLabel italics = new JLabel("Intalic text");
		
		fontInfo.add(size);
		fontInfo.add(fontSize);
		fontInfo.add(bolded);
		fontInfo.add(bold);
		fontInfo.add(italics);
		fontInfo.add(italic);
		
		return fontInfo;
	}
	
	/** creates the bottom panel
	 * @author Dybdal
	 * @since 1.1
	 * @return Jpanel
	 */
	public JPanel createBottomPanel()	{
		JPanel bottom = new JPanel();
		ok = new JButton("Ok");
		cancel = new JButton("Cancel");
		defaults = new JButton("Default");
		
		bottom.add(ok);
		bottom.add(cancel);
		bottom.add(defaults);
		
		return bottom;
	}
	
	/** closes the frame
	 * @author Dybdal
	 * @since 1.1
	 */
	public void closeFrame()	{
		frame.dispose();
	}
}
